home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Collection of Tools & Utilities
/
Collection of Tools and Utilities.iso
/
tex
/
sortdemo.zip
/
BUBBLE.PAS
< prev
next >
Wrap
Pascal/Delphi Source File
|
1987-09-03
|
2KB
|
101 lines
{ K.L. Noell, fhw 03.Sep.87 }
PROGRAM BubbleSort_Demo (output);
CONST n = 639; { number of columns : x-coordinates }
range = 199; { actual values : y-coordinates }
VAR
i1: INTEGER;
num,loops,swaps,aloops,aswaps: REAL;
A: ARRAY [0..n] OF INTEGER;
PROCEDURE BubbleSort ( np: INTEGER );
VAR
i,ie,temp: INTEGER;
flag: BOOLEAN;
BEGIN
ie := np;
REPEAT
flag := FALSE;
ie := ie - 1;
FOR i:= 0 TO ie DO BEGIN
loops := loops + 1;
IF A[i] > A[i+1] THEN { swap }
BEGIN
flag := TRUE;
swaps := swaps + 1;
Plot (i,A[i],0);
Plot ((i+1),A[i+1],0);
temp := A[i];
A[i] := A[i+1];
A[i+1]:= temp;
Plot(i, A[i] ,3);
Plot((i+1),A[i+1],3);
END;
END;
UNTIL flag = FALSE;
END; { BubbleSort }
{ ----------------------------------------- }
Begin { Mainprogram for BubbleSort_Demo }
HiRes;
HiResColor (Magenta);
FOR i1:=1 TO n DO BEGIN
num := range*RANDOM;
A [i1] := TRUNC (num);
Plot (i1,A[i1],15);
END;
GraphBackground (Magenta);
Palette (2);
{Sorting start:}
loops := 0;
swaps := 0;
DELAY (1000);
BubbleSort (n);
aloops := loops;
aswaps := swaps;
writeln (' Bubble Sort a) Loops,Swaps: ',loops,swaps);
writeln;
writeln ('b) Press any key to process with an array already sorted,');
writeln (' but in opposite direction.');
REPEAT UNTIL KeyPressed;
Hires;
GraphBackground(6);
Palette(2);
FOR i1:=1 TO n DO BEGIN
num := (n-i1)/(n/range);
A[i1] := TRUNC (num);
Plot (i1,A[i1],15);
END;
loops := 0;
swaps := 0;
DELAY (1000);
BubbleSort (n);
REPEAT UNTIL KeyPressed;
writeln (' Bubble Sort a) Loops,Swaps: ',aloops,aswaps);
writeln (' Bubble Sort b) Loops,Swaps: ',loops,swaps);
writeln;
writeln (' Press any key to exit.');
REPEAT UNTIL KeyPressed;
TextMode;
end. { BubbleSort_Demo }